#include <stdio.h>
 #include <pthread.h>
 #include <sys/types.h>
#include <unistd.h>
static int beginnum=1000;
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
static void* thrRead(void* arg)
{
        while (1){
                pthread_rwlock_wrlock(&rwlock);
                printf("pid:%d, tid:%lu,function:%s beginnum=%d\n",
                                        getpid(),pthread_self(),__FUNCTION__,beginnum);
                pthread_rwlock_unlock(&rwlock);
                usleep(4000);
        }
        return NULL;
}
static void* thrWrite(void* arg)
{
        while (1){
                pthread_rwlock_rdlock(&rwlock);
                beginnum++;
                printf("pid:%d, tid:%lu,function:%s beginnum=%d\n",
                                        getpid(),pthread_self(),__FUNCTION__,beginnum);
                pthread_rwlock_unlock(&rwlock);
                usleep(2000);
        }
        return NULL;
}
int main(int argc, char const *argv[])
{
        int i = 0;
        pthread_t tid[8];
        for (i = 0; i < 5; i++){
                pthread_create(&tid[i],NULL,thrRead,NULL);
        }
        for ( ; i < 8; i++){
                pthread_create(&tid[i],NULL,thrWrite,NULL);
        }
        for (i = 0; i < 8; i++)
        {
                pthread_join(tid[i], NULL);
        }
        pthread_exit(NULL);
        return 0;
}
